﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EvoAlgLib
{
    public class GPFactory: IndFactory
    {
        private String type;
        private double p_mutate;
        private int _depth;
        private FuncFactory _factory;


        public GPFactory(int depth)
        {
            this._depth = depth;
            this._factory = new FuncFactory();
        }

        private Node buildTree(int depth)
        {
            if (depth > 0)
            {
                if (depth == 1)
                {
                    Node leaf = new Node(this._factory.randomLiteral());
                    leaf.setDepth(1);
                    return leaf;
                }

                Func f = this._factory.randomFunc();
                Node n = new Node(f);
                n.setDepth(depth);


                for (int i = 0; i < f.getArity(); i++)
                {
                    n.setChild(this.buildTree(depth - 1), i);
                    n.getChildren()[i].setParent(n);
                }

                return n;
            }
            return null;
        }

        

        public void setType(String t)
        {
            this.type = t;
        }
        public void setPMutate(double pm)
        {
            this.p_mutate = pm;
        }

        public Individual getIndividual()
        {
            Node root = this.buildTree(this._depth);
            root.setParent(null);
            return new GPIndividual(root, p_mutate);
        }
        public Individual getIndividual(Individual other)
        {
            return null;
        }
    }
}
